/**
 * 检测参数的合法性，如果校验成功则方法返回true
 * @param {*} param0
 * @returns true
 */ import * as url from "url";
import Vue from "vue";

const checkParams: (pars: {
  url: string;
  method: "GET" | "POST";
}) => boolean = (pars = { url: "", method: "GET" }): boolean => {
  if (!pars.url) {
    throw new Error("url must be not null");
  }
  if (
    pars.method.toUpperCase() !== "GET" &&
    pars.method.toUpperCase() !== "POST"
  ) {
    throw new Error(
      'Illegal request method, Methods can only be "GET" or "POST"'
    );
  }
  return true;
};

const doReq = (
  vm: Vue,
  method: "GET" | "POST",
  pars: any
): Promise<any> | null => {
  if (method.toUpperCase() === "GET") {
    return vm.$get(pars);
  } else if (method.toUpperCase()) {
    return vm.$post(pars);
  }
  return null;
  // return vm[`$${pars.method.toLowerCase()}`](pars)
};
/**
 * 普通的加载数据方法，如加载列表、初始化数据
 * @param {*} param0
 */

export const loadData = (
  pars: {
    url: string;
    method: "GET" | "POST";
    data: any;
    beforeRequest: any;
    afterRequest: any;
  },
  vueInstance: Vue
): Promise<any> => {
  if (checkParams({ url: pars.url, method: pars.method })) {
    return doReq(
      vueInstance,
      pars.method.toUpperCase() as any,
      pars
    ) as Promise<any>;
  }
  throw new Error("params check failed");
};

/**
 * 模糊搜索方法
 * @param {*} param0
 */
export const likeSearch = (
  params: {
    url: string;
    method: "GET" | "POST";
    data: any;
    beforeRequest: any;
    afterRequest: any;
  },
  vueInstance: Vue
) => {
  if (checkParams({ url: params.url, method: params.method })) {
    return doReq(
      vueInstance,
      params.method.toUpperCase() as any,
      params
    ) as Promise<any>;
  }
  throw new Error("params check failed");
};

/**
 * 添加一条数据
 * @param {*} param0
 */
export const addItem = (
  params: {
    url: string;
    method: "GET" | "POST";
    data: any;
    beforeRequest: any;
    afterRequest: any;
  },
  vueInstance: Vue
) => {
  if (checkParams({ url: params.url, method: params.method })) {
    return doReq(
      vueInstance,
      params.method.toUpperCase() as any,
      params
    ) as Promise<any>;
  }
  throw new Error("params check failed");
};

/**
 * 删除n条数据
 * @param {*} items
 * @param {*} param1
 */
export const deleteItem = (
  params: {
    url: string;
    method: "GET" | "POST";
    data: any;
    beforeRequest: any;
    afterRequest: any;
  },
  vueInstance: Vue
) => {
  if (checkParams({ url: params.url, method: params.method })) {
    return doReq(
      vueInstance,
      params.method.toUpperCase() as any,
      params
    ) as Promise<any>;
  }
  throw new Error("params check failed");
};

/**
 * 更新一条数据
 * @param {*} item
 * @param {*} param1
 */
export const updateItem = (
  params: {
    url: string;
    method: "GET" | "POST";
    data: any;
    beforeRequest: any;
    afterRequest: any;
  },
  vueInstance: Vue
) => {
  if (checkParams({ url: params.url, method: params.method })) {
    return doReq(
      vueInstance,
      params.method.toUpperCase() as any,
      params
    ) as Promise<any>;
  }
  throw new Error("params check failed");
};
